/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, {Component} from 'react';
import {
    Button,
    Platform,
    StyleSheet,
    Text,
    StatusBar,
    Image,
    FlatList,
    Dimensions,
    TouchableOpacity,
    View
} from 'react-native';

const {width: sw, height: sh} = Dimensions.get('window');

export default class App extends Component {
    constructor() {
        super();
        this.state = {
            text: 'text'
        };
    }

    render() {
        return (
            <View>
                <View style={{flexDirection: 'row', margin: 10}}>
                    <Button title={'GET'} onPress={this._get}/>
                    <View style={{marginLeft: 5}}/>
                    <Button title={'POSTJSON'} onPress={this._postJson}/>
                    <View style={{marginLeft: 5}}/>
                    <Button title={'POSTForm'} onPress={this._postForm}/>
                </View>

                <Text style={{marginTop: 10, fontSize: 16}}>
                    {this.state.text}
                </Text>
            </View>
        );
    }

    _postForm = () =>{
        let url = "https://admin.gototrip.com.cn:28892/sight/rest/sight/queryDestination?serviceVersion=1.5";
        fetch(url, {
            method: 'POST',
            headers: {
                'Accept': 'application/json;charset=UTF-8',
                "Content-Type": "application/x-www-form-urlencoded",
            },
            body:"destinationName=广州&destinationLevel=2",
        }).then(resp => {
            console.log(resp);
            this.setState({text: JSON.stringify(resp)});
            return resp.json();
        }).then(data => {
            this.setState({text: this.state.text + '\n\n\ndata:' + JSON.stringify(data)})
        }).catch(err => {
            console.log(err);
            this.setState({text: this.state.text + '\n\n\nerror:' + err})
        });
    };

    _postJson = () => {
        let url = "https://admin.gototrip.com.cn:28999/supers/rest/superApp1.4/queryDestinationSumSupers?serviceVersion=1.5";
        fetch(url, {
            method: 'POST',
            headers: {
                'Accept': 'application/json;charset=UTF-8',
                'Content-Type': 'application/json;charset=UTF-8'
            },
            body: JSON.stringify({
                'destinationCode': '100000',
                'destinationLevel': 0
            }),
        }).then(resp => {
            console.log(resp)
            this.setState({text: JSON.stringify(resp)});
            return resp.json();
        }).then(data => {
            this.setState({text: this.state.text + '\n\n\ndata:' + JSON.stringify(data)})
        }).catch(err => {
            console.log(err);
            this.setState({text: this.state.text + '\n\n\nerror:' + err})
        });
    };

    _get = () => {
        let url = 'https://admin.gototrip.com.cn:28999/source/rest/praiseApp/queryePraiseDetail/1/SuperVote?serviceVersion=1.5';
        let url2 = 'http://192.168.20.68/11.txt';
        fetch(url).then(resp => {
            console.log(resp)
            this.setState({text: JSON.stringify(resp)})
            return resp.text();
        }).then(data => {
            // console.log(data);
            this.setState({text: this.state.text + '\n\n\ndata:' + data})
        }).catch(error => {
            console.log(error);
            this.setState({text: this.state.text + '\n\n\nerror:' + error})
        });
    }
}

